package cn.usian.service.employeeserviceimpl;

import cn.usian.common.R;
import cn.usian.dao.EmployeeMapper;
import cn.usian.entity.Employee;
import cn.usian.exception.NameException;
import cn.usian.service.EmployeeService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Date;


@Service
@Transactional
public class EmployeeServiceImpl implements EmployeeService {
    @Resource
    private EmployeeMapper employeeMapper;


    @Override
    public R<Employee> selectOne(Employee employee) {
        //获取密码
        String password = employee.getPassword();
        //转成数组,密码枷锁
        password= DigestUtils.md5DigestAsHex(password.getBytes());
        //根据用户名查询数据库
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getUsername,employee.getUsername());
        Employee emp = employeeMapper.selectOne(queryWrapper);
        //用户不存在的情况
        if(emp==null){
            return R.error("用户不存在");
        }
        //比较密码
        if(!emp.getPassword().equals(password)){
            return R.error("密码错误");
        }
        return R.success(emp);
    }

    @Override
    public void add(Employee employee) {
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getUsername,employee.getUsername());
        Employee emp=employeeMapper.selectOne(queryWrapper);
        if(emp !=null){
            throw new NameException(employee.getUsername());
        }
        employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());
        String psd = employee.getPassword();
        //转成数组,密码枷锁
        psd= DigestUtils.md5DigestAsHex(psd.getBytes());
        employee.setPassword(psd);


        employeeMapper.insert(employee);
    }

    @Override
    public Page<Employee> selectPage(Page<Employee> page1, LambdaQueryWrapper<Employee> queryWrapper) {
        return employeeMapper.selectPage(page1,queryWrapper);
    }

    @Override
    public void updateEmp(Employee employee) {
        employee.setUpdateTime(LocalDateTime.now());
        LambdaUpdateWrapper<Employee> updateWrapper = new LambdaUpdateWrapper<>();
        //判断姓名
        if(employee.getName()!=null && !employee.equals("")){
            updateWrapper.set(Employee::getName,employee.getName());
        }
        //判断账号
        if(employee.getUsername()!=null && !employee.equals("")){
            updateWrapper.set(Employee::getUsername,employee.getUsername());
        }
        //判断密码
        if(employee.getPassword()!=null && !employee.equals("")){
            updateWrapper.set(Employee::getPassword,employee.getPassword());
        }
        //判断手机号
        if(employee.getPhone()!=null && !employee.equals("")){
            updateWrapper.set(Employee::getPhone,employee.getPhone());
        }
        //判断性别
        if(employee.getSex()!=null && !employee.equals("")){
            updateWrapper.set(Employee::getSex,employee.getSex());
        }
        //判断身份证
        if(employee.getIdNumber()!=null && !employee.equals("")){
            updateWrapper.set(Employee::getIdNumber,employee.getIdNumber());
        }
        //判断状态
        if(employee.getStatus()!=null){
            updateWrapper.set(Employee::getStatus,employee.getStatus());
        }
        updateWrapper.eq(Employee::getId,employee.getId());
        employeeMapper.update(employee,updateWrapper);
    }

    @Override
    public R findById(Long id) {
        return R.success(employeeMapper.selectById(id));
    }


}
